package org.example;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

class MainTest {

    @Test
    void mainPrintsHelloAndWelcome() {
        // Capture the output。用于捕获标准输出
        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outContent));

        // Run the main method
        Main.main(new String[]{});

        // Verify the output
        String expectedOutput = "Hello and welcome!\ni = 1\ni = 2\ni = 3\ni = 4\ni = 5\n";
        String actualOutput = outContent.toString();
        System.out.println("Actual Output: " + actualOutput); // Print the actual output for debugging
        assertEquals(expectedOutput, actualOutput);
    }

    @Test
    void mainLoopRunsFiveTimes() {
        // Capture the output
        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outContent));

        // Run the main method
        Main.main(new String[]{});

        // Verify the loop runs five times
        long count = outContent.toString().lines().filter(line -> line.startsWith("i = ")).count();
        assertEquals(5, count);
    }
}